package cn.com.chengmandian.bike.service.impl;

import cn.com.chengmandian.bike.constants.BikeConstants;
import cn.com.chengmandian.bike.mapper.EbOperationNoparkingMapper;
import cn.com.chengmandian.bike.model.EbOperationNoparking;
import cn.com.chengmandian.bike.model.vo.OperationNoparkingVo;
import cn.com.chengmandian.bike.service.IEbOperationNoparkingService;
import cn.com.chengmandian.core.common.utils.LnglatUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 运营禁停区配置 服务实现类
 * </p>
 *
 * @author AutoGenerator
 * @since 2021-12-10
 */
@Service
public class EbOperationNoparkingServiceImpl extends ServiceImpl<EbOperationNoparkingMapper, EbOperationNoparking> implements IEbOperationNoparkingService {
    @Autowired
    private EbOperationNoparkingMapper operationNoparkingMapper;

    @Override
    public boolean isInNoparking(Integer areaId, BigDecimal lng, BigDecimal lat) {
        List<OperationNoparkingVo> list = operationNoparkingMapper.getNoparkingList(areaId);
        for(OperationNoparkingVo item : list) {
            if (LnglatUtil.isInPolygon(lng.doubleValue(), lat.doubleValue(), item.getLngs().split(","), item.getLats().split(","))) {
                return true;
            }
        }
        return false;
    }

    @Override
    public List<OperationNoparkingVo> getNoparkingList(Integer areaId) {
        return operationNoparkingMapper.getNoparkingList(areaId);
    }

    @Override
    public List<OperationNoparkingVo> getNearNoparkingList(Integer areaId, BigDecimal lat, BigDecimal lng) {
        BigDecimal minLat = lat.subtract(BikeConstants.NEAR_BIKE_RANG).setScale(6, BigDecimal.ROUND_HALF_UP);
        BigDecimal minLng = lng.subtract(BikeConstants.NEAR_BIKE_RANG).setScale(6, BigDecimal.ROUND_HALF_UP);
        BigDecimal maxLat = lat.add(BikeConstants.NEAR_BIKE_RANG).setScale(6, BigDecimal.ROUND_HALF_UP);
        BigDecimal maxLng = lng.add(BikeConstants.NEAR_BIKE_RANG).setScale(6, BigDecimal.ROUND_HALF_UP);
        return operationNoparkingMapper.getNearNoparkingList(areaId, minLat, maxLat, minLng, maxLng);
    }
}
